/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.Event;
import entity.Notification;
import entity.User;
import entity.WarningType;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

/**
 * retrive information about the logged user, such as its events
 * its notification and so on.
 * @author andrea
 */
@Named
@SessionScoped
public class UserDataLoader implements Serializable{
    
    private final User loggedUser;
    
    @EJB private EventManager eventManger;
    @EJB private UserManager  userManager;
    @EJB private NotificationManager notificationManager;
    
    /**
     *
     */
    public UserDataLoader(){
      loggedUser = userManager.getLoggedUser();
    }
    
    /**
     *
     * @return
     */
    public User getloggedUser(){
     return this.loggedUser;
    }
    
    /**
     *
     * @param eventEntity
     * @return event ID
     */
    public long createEvent(Event eventEntity){
        
        eventManger.createEvent(eventEntity, this.getloggedUser());
        notificationManager.createInvitation(eventEntity);
        
        return eventEntity.getEventID();
    }
    
    /**
     *
     * @param eventEntity
     */
    public void deleteEvent(Event eventEntity){
        
        if(eventManger.isTheOwner(loggedUser,eventEntity)){
            eventManger.deleteEvent(eventEntity);
            notificationManager.createWarninForAllParticipants(eventEntity, WarningType.EVENTDELETED);
        } else {
            eventManger.removeParticipant(eventEntity, loggedUser);
            notificationManager.createWarninForOwner(eventEntity, WarningType.PARTICIPANTREMOVED);
        }
    }
    

    
    /**
     *
     * @param eventEntity
     */
    public void modifyevent(Event eventEntity){       
        WarningType type = this.findWhatCahnged(eventEntity);
        eventManger.modifyEvent(eventEntity);
        notificationManager.createWarninForAllParticipants(eventEntity, type);     
    }
    
    /**
     * Should be used to get notification of the logged user,
     * that list contain un-seen notification and the lasts
     * already read
     * @return a list of notification
     */
    public List<Notification> getMyNotification(){        
        return notificationManager.findAllUnseen(this.loggedUser.getEmail());
    }
    
    /**
     * Should be used to get the notification of the logged user
     * @return a list of event
     */
    public List<Event> getMyEvent(){
        eventManger.findEventForSpecificUser(this.loggedUser.getEmail());
        return null;
    }
    
    /**
     * Get an entity event and find what is changed before the modification,
     * 
     * @param eventEntity
     * @return type 
     */
    private WarningType findWhatCahnged(Event eventEntity){
        //TODO- find a way to define the warning type
        //what can change in an event?
        //location
        //become public or private
        //change description (maybe not useful to send a warning)
        //change address
        WarningType type = null;
        return type;
    }
    
   
}
